﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Web;

namespace AAA.Web
{
    public class ControlHelper
    {
        public static GridView CloneGridView(GridView gvSource)
        {
            GridView gvResult = new GridView();

            for (int i = 0; i < gvSource.Columns.Count; i++)
                gvResult.Columns.Add(gvSource.Columns[i]);

            Clone(gvResult, gvSource);
            return gvResult;
        }

        public static object Clone(object target, object source)
        {
            if (source == null)
                throw new ArgumentNullException("Source");
            if (source.GetType() != target.GetType())
                throw new ArgumentException("Type Mismatch");
            foreach (PropertyInfo p in source.GetType().GetProperties())
            {
                if (p.CanRead && p.CanWrite)
                    p.SetValue(target, p.GetValue(source, p.GetIndexParameters()), p.GetIndexParameters());
            }
            return target;
        }
        public static Control CopyControls(Control target, Control source)
        {
            foreach (Control c in source.Controls)
            {
                Control cc;
                try
                {
                    cc = Activator.CreateInstance(c.GetType()) as Control;
                }
                catch { continue; }
                cc = Clone(cc, c) as Control;
                cc = CopyControls(cc, c);
                target.Controls.Add(cc);
                if (c.Controls.Count != 0)
                {
                    Control d = new Control();
                    d = CopyControls(d, c);
                    target.Controls.Add(d);
                }
            }
            return target;
        }
/*
        public static void ScrollableTable(GridView gvSource)
        {
            ScrollableTable(gvSource, 0);
        }

        public static void ScrollableTable(GridView[] gvSources)
        {
            ScrollableTable(gvSources, 0);
        }

        public static void ScrollableTable(GridView gvSource, int iFixedCol)
        {

            ScrollableTable(new GridView[] { gvSource }, iFixedCol);
        }

        public static void ScrollableTable(GridView[] gvSources, int iFixedCol)
        {
 
            string strSuperTableScript = "$(function() { ";
            for (int i = 0; i < gvSources.Length; i++)
                strSuperTableScript += "      $(\"#" + gvSources[i].ClientID + "\").toSuperTable({ width: \"" + gvSources[i].Width + "\", height: \"" + gvSources[i].Height + "\", fixedCols: " + iFixedCol + "}); ";
            strSuperTableScript += "      $(\"#" + gvSource.ClientID + "\").toSuperTable({ width: \"" + gvSource.Width + "\", height: \"" + gvSource.Height + "\", fixedCols: " + iFixedCol + "}); ";
            strSuperTableScript += " });";

            if (HttpContext.Current.CurrentHandler is Page)
            {
                Page p = (Page)HttpContext.Current.CurrentHandler;

                if (ScriptManager.GetCurrent(p) != null)
                {
//                    ScriptManager.RegisterStartupScript(p, typeof(Page), "SuperTable" + gvSources[0].ClientID, strSuperTableScript, true);
                    ScriptManager.RegisterStartupScript(p, typeof(Page), "SuperTable" + gvSource.ClientID, strSuperTableScript, true);
                }
                else
                {
//                    p.ClientScript.RegisterStartupScript(typeof(Page), "SuperTable" + gvSources[0].ClientID, strSuperTableScript, true);
                    p.ClientScript.RegisterStartupScript(typeof(Page), "SuperTable" + gvSource.ClientID, strSuperTableScript, true);
                }
            }
        }
*/
        public static void ExecuteScript(string strKey, string strScript, bool addScriptTag)
        {
            if (HttpContext.Current.CurrentHandler is Page)
            {
                Page p = (Page)HttpContext.Current.CurrentHandler;

                if (ScriptManager.GetCurrent(p) != null)
                {
                    //                    ScriptManager.RegisterStartupScript(p, typeof(Page), "SuperTable" + gvSources[0].ClientID, strSuperTableScript, true);
                    ScriptManager.RegisterStartupScript(p, typeof(Page), strKey, strScript, addScriptTag);
                }
                else
                {
                    //                    p.ClientScript.RegisterStartupScript(typeof(Page), "SuperTable" + gvSources[0].ClientID, strSuperTableScript, true);
                    //p.ClientScript.RegisterStartupScript(typeof(Page), strKey, strScript, addScriptTag);
                    p.ClientScript.RegisterClientScriptBlock(typeof(Page), strKey, strScript, addScriptTag);
                }
            }
        }
    }
}
